/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author vietean
 */
public class Customers {

    private String file;
    Document doc;

    public Customers(String file) {
        this.file = file;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.parse(this.file);
        } catch (Exception e) {
        }
    }

    public boolean insert(Customer customer) {
        try {
            NodeList nodeList = doc.getElementsByTagName("Customers");
            Element root = null;
            for (int i = 0; i < nodeList.getLength(); ++i) {
                if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    root = (Element) nodeList.item(i);
                }
            }
            int autoIncreaseId = Integer.parseInt(root.getAttribute("autoid"));
            autoIncreaseId++;
            root.setAttribute("autoid", String.valueOf(autoIncreaseId));
            Element customerElement = doc.createElement("Customer");
            root.appendChild(customerElement);

            // Id
            Element Id = doc.createElement("Id");
            Id.setTextContent(String.valueOf(autoIncreaseId));
            customerElement.appendChild(Id);

            // Fullname
            Element Fullname = doc.createElement("Fullname");
            Fullname.setTextContent(customer.getFullname());
            customerElement.appendChild(Fullname);

            // Username
            Element Username = doc.createElement("Username");
            Username.setTextContent(customer.getUsername());
            customerElement.appendChild(Username);

            // Password
            Element Password = doc.createElement("Password");
            Password.setTextContent(customer.getPassword());
            customerElement.appendChild(Password);

            // PhoneNo
            Element PhoneNo = doc.createElement("PhoneNo");
            PhoneNo.setTextContent(customer.getPhoneNo());
            customerElement.appendChild(PhoneNo);

            // Address
            Element Address = doc.createElement("Address");
            Address.setTextContent(customer.getAddress());
            customerElement.appendChild(Address);

            // Birthday
            Element Birthday = doc.createElement("Birthday");
            Birthday.setTextContent(customer.getBirthday());
            customerElement.appendChild(Birthday);

            // Email
            Element Email = doc.createElement("Email");
            Email.setTextContent(customer.getEmail());
            customerElement.appendChild(Email);
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(this.file));
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            try {
                transformer.transform(source, result);
            } catch (TransformerException ex) {
                Logger.getLogger(Customers.class.getName()).log(Level.SEVERE, null, ex);
            }
            return true;
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(Customers.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Customer getLoginCustomer(String Username, String Password) {
        Customer c = new Customer();
        //get tat ca cac username
        NodeList nodeList = doc.getElementsByTagName("Username");

        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element eUsername = (Element) nodeList.item(i);

                if (eUsername.getTextContent().equals(Username)) {
                    Element parentNode = (Element) eUsername.getParentNode();
                    String text = "";

                    c.setUsername(Username);
                    //Lay password o cho nay
                    text = parentNode.getElementsByTagName("Password").item(0).getTextContent();
                    if( !text.equals(Password) )
                    {
                        //Neu ma ko dung password thi return NULL o day lien
                        return null;
                    }
                    c.setPassword(Password);

                    text = parentNode.getElementsByTagName("Id").item(0).getTextContent();
                    c.setId(Integer.parseInt(text));

                    text = parentNode.getElementsByTagName("Fullname").item(0).getTextContent();
                    c.setFullname(text);

                    text = parentNode.getElementsByTagName("PhoneNo").item(0).getTextContent();
                    c.setPhoneNo(text);

                    text = parentNode.getElementsByTagName("Address").item(0).getTextContent();
                    c.setAddress(text);

                    text = parentNode.getElementsByTagName("Birthday").item(0).getTextContent();
                    c.setBirthday(text);
                    break;
                } else {
                    return null;
                }
            }
        }
        return c;
    }
}
